import breaDcrumb from '@/components/breadcrumb';
import draggable from 'vuedraggable'
import {
  getLists,
  namingRulesave,
  updateSortNum,
  dateleNamingRule,
  QueryRepels,
  deletesRepels,
  saveRepel,
  QueryMoneyPage,
  deletesMoney,
  saveMoney
} from '@/api/basicConfig/CCTrules.js';
import { getcompany } from "@/api/common.js";
import { getUserInfo } from "@/utils/auth.js";
export default {
  components: {
    breaDcrumb,
    draggable
  },
  data() {
    return {
      UserInfo: '',
      companyId: '',
      companyIdList: [],
      contractTypelist: [{
        value: '产品命名规则',
        id: 0
      }, {
        value: '物料价格',
        id: 1
      }],
      currenindex: 0,
      addDialog: false,
      addText: '',
      addTypeName: '',
      addTypeId: '',
      leftList: [{
        itemName: '强度等级',
        id: 1
      },
      {
        itemName: '规范性能等级',
        id: 2
      },
      {
        itemName: '材料要求',
        id: 3
      },
      {
        itemName: '特殊要求',
        id: 4
      },
      {
        itemName: '外加剂要求',
        id: 5
      },
      {
        itemName: '其他要求',
        id: 6
      }

      ],
      leftItemList: [],
      type: [],
      title: '',
      upDateDialog: false,
      upDateText: '',
      upDateTextId: '',
      /*互斥*/
      deletesRepelsdata: [],
      righItemtlist: [],
      comData: [],
      /*价格*/
      navListindex: 0,
      navList: [{
        name: '信息价格',
        id: 1
      },
      {
        name: '内部价格',
        id: 2
      },
      {
        name: '市场价格',
        id: 3
      },
      ],
      money: '',
      time: '',
      tableParam: {
        namingRuleId: '',
        page: 1,
        pageSize: 10,
        type: 1
      },
      total: null,
      moneyData: [],
    }
  },
  async created() {
    this.UserInfo = await getUserInfo();
    this.init()
    this.initright()
    /**通过typeId为公司类型id查询所有公司 */
    getcompany({ typeId: "19da56b9e85648059e3cf1e7da497da4" }).then((res) => {
      this.companylist = res.data;
      if (this.UserInfo.adminStatus == 0) {
        this.companyId = this.UserInfo.companyId
      } else {
        this.companyId = res.data[0].id
      }
    });
  },
  watch: {
    tableParam: {
      handler() {
        this.getmoneyData()
      },
      deep: true,
      immediate: false
    }
  },
  /*
   *copy(data)深拷贝数据,返回拷贝完的数据
   *gettableData获取列表数据
   *load下载
   *SortNum保持序列号
   * UpdateOrDelete禁用，启用，删除
   * submitFromDate保存更新数据
   */
  methods: {
    choose(num) {
      this.currenindex = num;
    },
    initright() {
      QueryRepels().then(res => {
        this.righItemtlist = res.data
      })
    },
    init() {
      getLists().then(res => {
        this.leftItemList = res.data
        this.leftItemList[0].list.splice(0, 0, {
          id: "",
          name: "不选强度等级",
          sortNum: 0
        })
        console.log(JSON.parse(JSON.stringify(this.leftItemList)))
      })
    },
    resetFromDate() {
      this.addDialog = false
      this.addText = ''
      this.addTypeId = ''
      this.addTypeName = ''
    },
    addItem() {
      let _this = this
      _this.addTypeName = _this.leftList.find(function (obj) {
        return obj.id == _this.addTypeId;
      }).itemName;
      const data = {
        name: _this.addText,
        typeName: _this.addTypeName,
        typeId: _this.addTypeId
      }
      namingRulesave(data).then(() => {
        _this.resetFromDate()
        _this.init()
      })
    },
    showtext(data) {
      this.upDateText = data.name
      this.upDateTextId = data.id
      this.upDateDialog = true
    },
    upDatedate() {
      let _this = this
      const data = {
        name: _this.upDateText,
        id: this.upDateTextId
      }
      namingRulesave(data).then(() => {
        this.upDateDialog = false
        _this.resetFromDate()
        _this.init()
      })
    },
    drageEnd(item) {
      let key = item.map((item, index) => {
        let obj = new Object()
        obj.id = item.id
        obj.sortNum = index + 1
        return obj
      })
      updateSortNum(key).then(res => {
        console.log(res)
      })
    },
    deletetext(item) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'bottom_all_two',
        confirmButtonClass: 'bottom_all',
        type: 'warning'
      }).then(() => {
        dateleNamingRule({
          id: item.id
        }).then(res => {
          if (res.code == 1001) {
            this.init()
          }
        })
      }).catch(() => { });
    },
    DeletesRepel() {
      if (this.deletesRepelsdata.length == 0) {
        return
      }
      deletesRepels(this.deletesRepelsdata).then(() => {
        this.initright()
      })
    },
    DeletesallRepel() {
      if (this.righItemtlist.length == 0) {
        return
      }
      let key = this.righItemtlist.map(item => {
        return item.id
      })
      deletesRepels(key).then(() => {
        this.initright()
      })
    },
    delcomData(index) {
      this.comData.splice(index, 1)
    },
    addcomData() {
      let key = this.combination()
      if (this.comData.length > 2) {
        this.$message({
          message: '警告哦，最多添加单个条件',
          type: 'warning'
        });
        return
      }
      if (key.name == '') {
        this.$message({
          message: '警告哦，清选择数据后再添加为互斥条件',
          type: 'warning'
        });
        return
      }
      console.log(key)
      let obj = {
        name: key.name,
        ids: key.ids
      }
      this.comData.push(obj)
      this.type = []
      this.title = ''
    },
    combination() {
      let names = []
      let ids = []
      if (this.title != '') {
        this.leftItemList[0].list.forEach(item => {
          if (item.id == this.title) {
            ids.push(item.id)
            names.push(item.name)
          }
        })
      }
      this.leftItemList.forEach(item => {
        item.list.forEach(a => {
          if (this.type.indexOf(a.id) !== -1) {
            ids.push(a.id)
            names.push(a.name)
          }
        })
      })
      return {
        name: names.join(','),
        ids: ids.join(',')
      }
    },
    saveRepelData(num) {
      let key = {
        name: '',
        ids: ''
      }
      console.log(this.comData);
      if (num == 1) {
        key = this.combination()
      } else {
        if (this.comData.length < 2) {
          this.$message({
            message: '警告哦，互斥条件必须为两个条件以上',
            type: 'warning'
          });
          return
        }
        this.comData.forEach(item => {
          key.name = key.name + '(' + item.name + ')'
          key.ids = key.ids + item.ids + '%'
        })
      }
      saveRepel({
        ...key,
        type: num
      }).then(() => {
        this.initright()
        this.type = []
        this.title = ''
        if (num == 2) {
          this.comData = []
        }
      })
    },
    choosenavListindex(item, index) {
      this.navListindex = index
      console.log(index)
      this.tableParam.type = item.id
    },
    getmoneyData() {
      let radioName = []
      radioName = [...radioName, ...this.leftItemList[0]?.list, ...this.leftItemList[1]?.list, ...this.leftItemList[2]?.list, ...this.leftItemList[3]?.list, ...this.leftItemList[4]?.list, ...this.leftItemList[5]?.list]
      let name = radioName.find((res) => res.id == this.tableParam.namingRuleId).name
      this.tableParam.companyId = this.companyId
      QueryMoneyPage(this.tableParam).then(res => {
        this.moneyData = res.data.data
        this.moneyData.map((v) => {
          v.name = name
        })
        this.total = res.data.totalRows
      })
    },
    deletemoney(data) {
      let arr = []
      arr.push(data.id)
      this.$confirm('此操作将永久删除该信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'bottom_all_two',
        confirmButtonClass: 'bottom_all',
        type: 'warning'
      })
        .then(() => {
          deletesMoney(arr).then(() => {
            this.getmoneyData()
          })
        })
        .catch(() => { });
    },
    addmoney() {
      if (!this.tableParam.namingRuleId || !this.money || !this.time) {
        this.$message({
          message: '请添加完整的价格数据',
          type: 'warning'
        });
      } else {
        if (this.UserInfo.adminStatus == 0) {
          this.companyId = this.UserInfo.companyId
        }
        let company = this.$fromatTable(
          this.companylist,
          this.companyId,
          "id",
          "name"
        );
        let data = {
          company: company,
          companyId: this.companyId,
          namingRuleId: this.tableParam.namingRuleId, //产品id
          startTime: this.time, //开始时间
          money: this.money, //价格  单位分
          type: this.tableParam.type, //信息价格  1；内部价格 
        }
        saveMoney(data).then(() => {
          this.time = ''
          this.money = ''
          this.getmoneyData()
        })
      }
    }
  }

}